package com.zbf.auth.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/8/27 19:52
 * 描述:
 */
@Mapper
public interface MenuDao {

    /**
     * 获取所有的权限信息，以及权限对应的角色
     * @return
     */
    @Select("select CONCAT(br.role_code,\":\",bm.url ) urlRoleCode from base_menu bm inner join base_role_menu brm on brm.menu_id=bm.id INNER JOIN base_role br on br.id=brm.role_id where url!=null or url!=''")
    public List<Map<String,String>>  getAllMenus();

    /**
     * 根据URl 获取对应的角色列表
     * @param url
     * @return
     */
    @Select("select br.role_code roleCode from base_menu bm inner join base_role_menu brm on bm.id=brm.menu_id inner join base_role br on br.id=brm.role_id where bm.url=#{url} GROUP BY role_code")
    public List<String> getRoles(String url);

    @Select("SELECT DISTINCT bu.id userId,bu.loginName,bm.`code`,\n" +
            "bm.id menuId,bm.menuName,bm.url,bm.leval,bm.imagePath FROM base_user_role bur \n" +
            "INNER JOIN base_user bu       ON bu.id=bur.userId\n" +
            "INNER JOIN base_role_menu brm ON brm.role_id=bur.roleId\n" +
            "INNER JOIN base_menu bm       ON bm.id=brm.menu_id\n" +
            "WHERE bu.loginName=#{loginName} and bm.leval=1")
    public List<Map<String,Object>> getUserFirstMenu(String loginName);
    /**
     * 查询用户菜单子级
     * @param map
     * @return
     */
    @Select("SELECT DISTINCT bu.id userId,bu.loginName,bm.`code`,bm.parentCode,\n" +
            "bm.id menuId,bm.menuName,bm.url,bm.leval,bm.imagePath FROM base_user_role bur \n" +
            "INNER JOIN base_user bu       ON bu.id=bur.userId\n" +
            "INNER JOIN base_role_menu brm ON brm.role_id=bur.roleId\n" +
            "INNER JOIN base_menu bm       ON bm.id=brm.menu_id\n" +
            "WHERE bu.loginName=#{loginName} and bm.leval=#{leval} and bm.parentCode=#{parentCode}")
    public List<Map<String,Object>> getUserNextMenu(Map<String,Object> map);


    @Update("UPDATE base_menu SET menuName=#{menuName},url=#{url},imagePath=#{imagePath} WHERE `code`=#{code}")
    public boolean updateUserAuth(Map<String,Object> userData);

    @Insert("INSERT INTO base_menu (id,menuName,url,imagePath,`code`,parentCode,leval) VALUES(#{id},#{menuName},#{url},#{imagePath},#{code},#{parentCode},#{leval});")
    public boolean addUserAuth(Map<String,Object> userData);

    @Delete("delete from base_menu where code=#{code}")
    public boolean delmenuByCode(Map<String,Object> userData);

    @Delete("DELETE FROM base_role_menu WHERE menu_id=#{menuId}")
    public boolean delmenu_roleBymid(Map<String,Object> userData);


}
